import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_detail.dart';
import 'package:enjoy_plus/pages/house/house_form.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/location_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/index.dart';
import 'package:enjoy_plus/pages/notice/index.dart';
import 'package:enjoy_plus/pages/profile/index.dart';
import 'package:enjoy_plus/pages/tab_bar_page.dart';
import 'package:enjoy_plus/utils/toast.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    //如果配置的话,就按照这个配置的路由进行跳转
    routes: {
      '/': (context) => const TabBarPage(),
      '/detail': (context) => const NoticeDetail(),
      // '/login': (context) => const LoginPage(),
      // '/profile': (context) => ProfilePage(),
    },
    //路由的全局拦截
    //如果没有配置的话,就执行这个函数
    //可以根据不同的人为逻辑,进行路由跳转
    onGenerateRoute: (setting) {
      //先获取用户的Token
      //没有token的话就需要去登录页面
      //有token的话,就可以去Profile页面
      final token = TokenManager().getToken();
      if (token == null) {
        //提示用户登录
        ToastUtil.showInfo('去登录');
        return MaterialPageRoute(builder: (context) {
          return const LoginPage();
        });
      }

//对登录判断进行优化
//如果路由的切换是 onGenerateRoute实现的话
//参数的传递需要自己手动的传递
//通过Setting.arguments传递参数就可以
      if (setting.name == '/profile') {
        return MaterialPageRoute(builder: (context) {
          return ProfilePage(info: setting.arguments as Map);
        });
      }
//获取房屋信息
      if (setting.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }

//添加房屋
      if (setting.name == '/location_list') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }

      //添加访问页面
      if (setting.name == '/build_list') {
        final point = (setting.arguments as Map)['point'];
        return MaterialPageRoute(
            builder: (context) => BuildingList(point: point));
      }
      //跳转到房间
      if (setting.name == '/room_list') {
        final point = (setting.arguments as Map)['point'];
        final building = (setting.arguments as Map)['building'];
        // print('$point,$building');
        return MaterialPageRoute(
            builder: (context) => RoomList(point: point, building: building));
      }
      //跳转到房间详情
      if (setting.name == '/house_form') {
        return MaterialPageRoute(
            builder: (context) => HouseForm(params: setting.arguments as Map));
      }
      //跳转到房屋详情
      if (setting.name == '/house_detail') {
        return MaterialPageRoute(
            builder: (context) =>
                HouseDetail(id: (setting.arguments as Map)['id']));
      }

      return null;
    },
    initialRoute: '/',
  ));
}
